﻿using System;

namespace Library.OrmEnterpriseEntity.TableInfo {
	///<summary>
	///</summary>
	public class DateColumn : DateTimeColumn {
		///<summary>
		///</summary>
		///<param name="metadata"></param>
		///<param name="table"></param>
		public DateColumn(DbColumnView metadata, DbTable table)
			: base(metadata, table) {
		}
		public override string ToString(string format, IFormatProvider formatProvider) {
			if (Value == DefaultDate) return String.Empty;
			return Value.ToString(format, formatProvider);
		}
		///<summary>
		///</summary>
		///<param name="value"></param>
		///<returns></returns>
		public static implicit operator DateColumn(DateTime value) {
			return new DateColumn(DateColumnView.Null, DbTable.Null) { Value = value };
		}
		internal static readonly Type DateColumnType = typeof(DateColumn);
		///<summary>
		///</summary>
		public static readonly DateTime DefaultDate = DefaultDateTime.Date;


		protected override DateTime Default {
			get { return DefaultDate; }
		}

		protected override void SetValue(ref DateTime oldValue, DateTime newValue) {
			base.SetValue(ref oldValue, newValue.Date);
		}

		public override string ToString() {
			if (Value == DefaultDate)
				return String.Empty;
			return Value.ToShortDateString();
		}
		protected override bool Equal(DateTime t1, DateTime t2) {
			return t1.Date == t2.Date;
		}
		//public override DbType DbType {
		//  get { return DbType.Date; }
		//}
		protected override DateTime ConvertFrom(object value) {
			return base.ConvertFrom(value).Date;
		}

	}
}
